package com.cn.yunhe.rentcars.web;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.mapper.CarMapper;
import com.cn.yunhe.rentcars.mapper.RepairMapper;
import com.cn.yunhe.rentcars.model.Car;
import com.cn.yunhe.rentcars.model.Repair;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
public class RepairController {
	
	//增加	URL：maintain/add method:post
	//删除	URL：maintain/delete method:delete
	//查询	URL：maintain/select method:get
	//修改	URL：maintain/query method:put
	
	@Autowired
	private RepairMapper repairMapper;
	@Autowired
	private CarMapper carMapper ;
/*	@RequestMapping(value="/cspage", method=RequestMethod.GET)
	public ModelAndView cspage() {	
		return new ModelAndView("cs");
	}*/
	
	@RequestMapping(value="/repair-list", method=RequestMethod.GET)
	public ModelAndView repairList() {	
		return new ModelAndView("repair-list");
	}

	@RequestMapping(value="/repair-add", method=RequestMethod.GET)
	public ModelAndView showAddRepair() {	
		return new ModelAndView("repair-add");
	}
	
	@RequestMapping(value="/repair-edit", method=RequestMethod.GET)
	public ModelAndView showEditRepair(@RequestParam String recordid) {	
		System.out.print("1+1=2");
		Repair repair = repairMapper.getRepairById(recordid);
		
		Map<String,Repair> modelMap = new HashMap<String,Repair>();
		modelMap.put("repair", repair);
		
		return new ModelAndView("repair-edit",modelMap);		
	}
	
	@RequestMapping(value="/getrepairbycarid", method=RequestMethod.GET)
	public PageInfo<Repair> getRepairbycarid(@RequestParam String carid,@RequestParam  int pageNum,@RequestParam  int pageSize){
	      PageHelper.startPage(pageNum, pageSize);
	      List<Repair> repairList = repairMapper.getRepair(carid);
	      PageInfo<Repair> page = new PageInfo<Repair>(repairList);
	      return page;
	}
	
	
	/**
	 * 添加新用户
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/addRepair", method=RequestMethod.POST)
	public void addRepair(@RequestBody Repair repair){		
		repair.setRecordid(UUID.randomUUID().toString());
		repairMapper.addRepair(repair);
		Car car = new Car ();
		car.setCarcode(repair.getCarid());
		
		if(repair.getReturntime()!=""){		
			car.setCarstate(6);
		}else{
			car.setCarstate(8);			
		}
		carMapper.setCarAllstate(car);
	}
	
	/**
	 * 修改用户
	 * 
	 * @param userid
	 */
	@RequestMapping(value="/repair/{recordid}", method=RequestMethod.PUT)
	public void editRepair(@PathVariable("recordid") String recordid,@RequestBody Repair repair){
		repair.setRecordid(recordid);
		repairMapper.editRepair(repair);
		if(repair.getReturntime()!=""){
			Car car = new Car ();		
			car.setCarcode(repair.getCarid());
			car.setCarstate(6);
			carMapper.setCarAllstate(car);
		}
		
	}

	/**
	 * 删除用户信息
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/delRepair", method=RequestMethod.POST)
	public void delRepair(@RequestParam String recordid){ 
		repairMapper.delRepair(recordid);
	}
	
	/**
	 * 删除用户信息
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/delRepairs", method=RequestMethod.POST)
	public void delRepairs(@RequestParam String recordid){ 
		List<String> recordidList = Arrays.asList(recordid.split(","));  		
		repairMapper.delRepairs(recordidList);
	}
	
	
	/**
	 * 搜索查询用户列表信息
	 * 
	 * @param username
	 * @param draw
	 * @param start
	 * @param length
	 * @return
	 */
	@RequestMapping(value="/repair", method=RequestMethod.GET)
	public Map<String,Object> getLikeRepair(@RequestParam String datemin,@RequestParam String datemax,@RequestParam String carid,@RequestParam int draw,@RequestParam int start,@RequestParam int length){
		  
		  Map<String,String> params =  new HashMap<String,String>();
		  params.put("carid", carid);
		  params.put("datemin", datemin);
		  params.put("datemax", datemax);
		
		  int pageNum = (start/length)+1;
		  int pageSize = length;		 
		  
	      PageHelper.startPage(pageNum, pageSize);
	      List<Repair> repairList = repairMapper.searchRepair(params);
	      PageInfo<Repair> page = new PageInfo<Repair>(repairList);
	      
	      Map<String,Object> rsMap = new HashMap<String,Object>();
	      rsMap.put("draw",draw);
	      rsMap.put("data", page.getList());	      
	      rsMap.put("recordsTotal", page.getTotal());
	      rsMap.put("recordsFiltered", page.getTotal());
	      return rsMap;
	}

}
